package com.lin.services.impl;

import com.lin.dao.AuthorityDao;
import com.lin.dao.StoreDao;
import com.lin.dao.UserDao;
import com.lin.pojo.Authority;
import com.lin.pojo.Role;
import com.lin.pojo.Store;
import com.lin.pojo.User;
import com.lin.services.StoreService;
import com.lin.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName    StoreServiceImpl
 * Package      com.lin.services.impl
 *
 * @author hellcat
 * Date         2020/2/24
 * Description:
 * 🐾
 */
@Service
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreDao storeDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AuthorityDao authorityDao;

    @Override
    public Store getStoreInfoById() {
        Integer storeId = ShiroUtil.getUser().getStoreId();
        return storeDao.getStoreInfoById(storeId);
    }

    @Override
    public Map<String, Object> listArea() {
        Map<String, Object> map = new HashMap<>();
        map.put("code", false);
        map.put("msg", "获取失败");
        List<Map<String, Object>> dataList = storeDao.listArea();
        if (dataList != null && dataList.size() != 0) {
            map.clear();
            map.put("data", dataList);
            map.put("code", true);
        }
        return map;
    }

    @Override
    public Map<String, Object> updateStore(Store store) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", false);
        resultMap.put("msg", "修改失败");
        if (store == null || store.getId() == null) {
            return resultMap;
        }
        int i = storeDao.updateStore(store);
        if (i == 1) {
            resultMap.clear();
            resultMap.put("code", true);
        }
        return resultMap;
    }

    @Override
    public Map<String, Object> insertStore(Store store) {
        Map<String, Object> map = new HashMap<>();
        try {
            store.setContactsId(ShiroUtil.getUserId());
            storeDao.insertStore(store);
            User user = ShiroUtil.getUser();
            user.setStoreId(store.getId());
            userDao.updateUserStoreId(user);
            Role role = new Role();
            role.setRole("经理");
            role.setStoreId(store.getId());
            authorityDao.insertRole(role);
            List<String> list = authorityDao.getAllAuthority();
            for (String authorityId : list) {
                authorityDao.insertRoleAuthority(role.getId().toString(), authorityId);
            }
            userDao.insertUserRole(user.getId(),role.getId().toString());
            ShiroUtil.clearAllCachedInfo(user.getAccountNumber());
            map.put("code", true);
        } catch (Exception e) {
            map.put("code", false);
            map.put("msg", "创建失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return map;
    }
}
